<?php /* CALENDAR $Id: addedit.php,v 1.48.4.7 2007/02/14 23:35:28 nybod Exp $ */
$event_id = intval( dPgetParam( $_GET, "event_id", 0 ) );
$is_clash = isset($_SESSION['event_is_clash']) ? $_SESSION['event_is_clash'] : false;

// check permissions
if (!$canEdit) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// get the passed timestamp (today if none)
$date = dPgetParam( $_GET, 'date', null );

// load the record data
$obj = new CEvent();

if ($is_clash) {
	$obj->bind($_SESSION['add_event_post']);
}
else if ( !$obj->load( $event_id ) && $event_id ) {
	$AppUI->setMsg( 'Event' );
	$AppUI->setMsg( "invalidID", UI_MSG_ERROR, true );
	$AppUI->redirect();
}

// load the event types
$types = dPgetSysVal( 'EventType' );

// Load the users
$perms =& $AppUI->acl();
$q  = new DBQuery;
$q->addTable('users', 'u');
$q->addTable('contacts', 'con');
$q->addQuery('user_id, CONCAT_WS(" " , contact_first_name, contact_last_name)');
$q->addWhere('user_contact = contact_id');
$q->addWhere('user_id <> 1');
$users = $q->loadHashList();
//$users = $perms->getPermittedUsers();

// Load the assignees
$assigned = array();
if ($is_clash) {
	$assignee_list = $_SESSION['add_event_attendees'];
	if (isset($assignee_list) && $assignee_list) {
		$ids = explode(',', $assignee_list);
		foreach($ids as $id) {
			$id = intval($id);
			$assigned[$id] = $users[$id];
			unset($users[$id]);
		}
	} else {
	}
} else if ( $event_id == 0 ) {
	$assigned[$AppUI->user_id] = $AppUI->user_first_name.' '.$AppUI->user_last_name;
	unset($users[$AppUI->user_id]);
} else {
	$assigned = $obj->getAssigned();
	foreach ($assigned as $id => $name) {
		unset($users[$id]);
	}
}
// Now that we have loaded the possible replacement event,  remove the stored
// details, NOTE: This could cause using a back button to make things break,
// but that is the least of our problems.
if ($is_clash) {
 	unset($_SESSION['add_event_post']);
	unset($_SESSION['add_event_attendees']);
	unset($_SESSION['add_event_mail']);
	unset($_SESSION['add_event_clash']);
	unset($_SESSION['event_is_clash']);
}
if ($_GET['event_project']) {
	$obj->event_project = $_GET['event_project'];
}

// setup the title block
$titleBlock = new CTitleBlock( $event_id ? 'Edit Event' : 'Add Event' );
$titleBlock->addButton( 'month view', 'index.php?m=calendar' );
$titleBlock->addCell();
$titleBlock->show();

// format dates
$df = $AppUI->getPref('SHDATEFORMAT');

// pull projects
require_once( $AppUI->getModuleClass( 'projects' ) );
$q =& new DBQuery;
$q->addTable('projects', 'p');
$q->addQuery('p.project_id, p.project_name');

$prj =& new CProject;
$allowedProjects = $prj->getAllowedSQL($AppUI->user_id);

if (count($allowedProjects)) {
	$prj->setAllowedSQL($AppUI->user_id, $q);
}
$q->addOrder('project_name');

$all_projects = '(' . $AppUI->_('All', UI_OUTPUT_RAW) . ')';
$projects = arrayMerge( array( 0 => $all_projects ), $q->loadHashList() );

if ($event_id || $is_clash) {
	$start_date = intval( $obj->event_start_date ) ? new CDate( $obj->event_start_date ) : null;
	$end_date = intval( $obj->event_end_date ) ? new CDate( $obj->event_end_date ) : $start_date;
} else {
	$start_date = new CDate( $date );
	$start_date->setTime( 8,0,0 );
	$end_date = new CDate( $date );
	$end_date->setTime( 17,0,0 );
}

$inc = intval(dPgetConfig('cal_day_increment')) ? intval(dPgetConfig('cal_day_increment')) : 30;
if (!$event_id && !$is_clash)
{

	$seldate = new CDate( $date );
	// If date is today, set start time to now + inc
	if ($date == date('Ymd'))
	{
		$h = date('H');
		// an interval after now.
		$min = intval(date('i') / $inc) + 1;
		$min *= $inc;
		if ($min > 60)
		{
			$min = 0;
			$h++;
		}
	}
	if ($h && $h < dPgetConfig('cal_day_end'))
	{
		$seldate->setTime($h, $min, 0);
		$obj->event_start_date = $seldate->format(FMT_TIMESTAMP);
		$seldate->addSeconds( $inc * 60 );
		$obj->event_end_date = $seldate->format(FMT_TIMESTAMP);
	}
	else
	{
		$seldate->setTime(dPgetConfig('cal_day_start'),0,0);
		$obj->event_start_date = $seldate->format(FMT_TIMESTAMP);
		$seldate->setTime(dPgetConfig('cal_day_end'),0,0);
		$obj->event_end_date = $seldate->format(FMT_TIMESTAMP);
	}
}

$recurs =  array (
	'Never',
	'Hourly',
	'Daily',
	'Weekly',
	'Bi-Weekly',
	'Monthly',
	'Quarterly',
	'Semi-Annually',
	'Annually'
);

$remind = array (
	"900" => '15 mins',
	"1800" => '30 mins',
	"3600" => '1 hour',
	"7200" => '2 hours',
	"14400" => '4 hours',
	"28800" => '8 hours',
	"56600" => '16 hours',
	"86400" => '1 day',
	"172800" => '2 days'
);

// build array of times in 30 minute increments
$times = array();
$t = new CDate();
$t->setTime( 0,0,0 );
if (!defined('LOCALE_TIME_FORMAT'))
  define('LOCALE_TIME_FORMAT', '%I:%M %p');
//$m clashes with global $m (module)
for ($minutes=0; $minutes < ((24 * 60) / $inc); $minutes++) {
	$times[$t->format( "%H%M%S" )] = $t->format( LOCALE_TIME_FORMAT );
	$t->addSeconds( $inc * 60 );
}
?>

<script language="javascript">
function doSaveEvent(){
	var form = document.editFrm;
	if (form.event_title.value.length < 1) {
		alert('<?=$AppUI->_('Please enter a valid event title',  UI_OUTPUT_JS)?>');
		form.event_title.focus();
		return;
	}
	if (form.event_start_date.value.length < 1){
		alert('<?=$AppUI->_("Please enter a start date", UI_OUTPUT_JS)?>');
		form.event_start_date.focus();
		return;
	}
	if (form.event_end_date.value.length < 1){
		alert('<?=$AppUI->_("Please enter an end date", UI_OUTPUT_JS)?>');
		form.event_end_date.focus();
		return;
	}
	if ( (!(form.event_times_recuring.value>0))
		&& (form.event_recurs[0].selected!=true) ) {
		alert("<?=$AppUI->_('Please enter number of recurrences', UI_OUTPUT_JS)?>");
		form.event_times_recuring.value=1;
		form.event_times_recuring.focus();
		return;
	}
	// Ensure that the assigned values are selected before submitting.
	var assigned = form.assigned;
	var len = assigned.length;
	var users = form.event_assigned;
	users.value = "";
	for (var i = 0; i < len; i++) {
		if (i)
			users.value += ",";
		users.value += assigned.options[i].value;
	}
	form.submit();
}

Calendar.enabled = true;
</script>
<form name="editFrm" action="?m=calendar" method="post">
	<input type="hidden" name="dosql" value="do_event_aed" />
	<input type="hidden" name="event_id" value="<?=$event_id?>" />
	<input type="hidden" name="event_project" value="0" />
	<input type="hidden" name="event_assigned" value="" />

<table cellspacing="0" cellpadding="7" border="0" width="100%" class="infopanel">
<tr>
	<td>
		<input type="button" value="<?=$AppUI->_( 'Save' )?>" class="button" onClick="doSaveEvent()">
		<input type="button" value="<?=$AppUI->_( 'Cancel' )?>" class="button" onclick="history.back();">
	</td>
	<td align="right">
	<?
		if ($event_id) showButton( 'view this event', 'index.php?m=calendar&a=view&event_id='.$event_id );
	?>
	</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="subinfopanel">
<tr>
<td valign="top">

<table cellspacing="0" cellpadding="3" border="0" width="100%">
<tr>
	<td align="right" nowrap="nowrap"><?=$AppUI->_( 'Title' )?>:</td>
	<td>
		<input type="text" class="text" name="event_title" value="<?=@$obj->event_title?>"
				maxlength="255" style="width: 88%">
	</td>
</tr>

<tr>
	<td align="right"><?=$AppUI->_('Type')?>:</td>
	<td><?=arraySelect( $types, 'event_type', 'size="1" class="text" style="width: 90%"', @$obj->event_type, true )?></td>
</tr>

<tr>
	<td align="right"><?=$AppUI->_('Project')?>:</td>
	<td><?=arraySelect( $projects, 'event_project', 'size="1" class="text" style="width: 90%"', @$obj->event_project )?></td>
</tr>

<tr>
	<td align="right" nowrap="nowrap"><?=$AppUI->_( 'Start Date' )?>:</td>
	<td nowrap="nowrap">
		<input id="idDateStart" type="hidden" name="event_start_date" value="<?=$start_date ? $start_date->format( FMT_TIMESTAMP_DATE ) : ''?>">
		<input id="idDateStartD" type="text" name="start_date" value="<?=$start_date ? $start_date->format( $df ) : ''?>" class="text" disabled="disabled" style="width: 70px">
		<img id="idDateStartB" class="ico" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>"/>
		&nbsp;
		<?=$AppUI->_( 'at' )?>
		<?=arraySelect( $times, 'start_time', 'size="1" class="text"', $start_date->format( "%H%M%S" ) )?>
	</td>
</tr>

<tr>
	<td align="right" nowrap="nowrap"><?=$AppUI->_( 'End Date' )?>:</td>
	<td nowrap="nowrap">
		<input id="idDateEnd" type="hidden" name="event_end_date" value="<?=$end_date ? $end_date->format( FMT_TIMESTAMP_DATE ) : ''?>">
		<input id="idDateEndD" type="text" name="end_date" value="<?=$end_date ? $end_date->format( $df ) : ''?>" class="text" disabled="disabled" style="width: 70px">
		<img id="idDateEndB" class="ico" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>"/>
		&nbsp;
		<?=$AppUI->_( 'at' )?>
		<?=arraySelect( $times, 'end_time', 'size="1" class="text"', $end_date->format( "%H%M%S" ) )?>
	</td>
</tr>
<tr>
	<td align="right" nowrap="nowrap"><?=$AppUI->_( 'Recurs' )?>:</td>
	<td><?=arraySelect( $recurs, 'event_recurs', 'size="1" class="text"', $obj->event_recurs, true )?>&nbsp;x&nbsp;
		<input type="text"  name="event_times_recuring" value="<?=((isset($obj->event_times_recuring))?($obj->event_times_recuring):'1')?>" maxlength="2" size=3> <?=$AppUI->_( 'times' )?>
	</td>
</tr>
<?php /* FUNCTIONALITY NOT YET ENABLED ?>
<tr>
	<td align="right" nowrap="nowrap"><?=$AppUI->_( 'Remind Me' )?>:</td>
	<td><?=arraySelect( $remind, 'event_remind', 'size="1" class="text"', $obj['event_remind'] )?> <?=$AppUI->_( 'in advance' )?></td>
</tr>
<?php */ ?>
<tr>
	<td>&nbsp;</td>
	<td style="padding-left: 0px">
	<table cellspacing="2" cellpadding="0" border="0">
	<tr>
		<td align="right">
			<input id="idEventPrivate" type="checkbox" value="1" name="event_private" <?=(@$obj->event_private ? 'checked' : '')?>>
		</td>
		<td nowrap="nowrap">
			<label for="idEventPrivate"><?=$AppUI->_( 'Private Entry' )?></label>
		</td>
	</tr>
	<tr>
		<td align="right">
			<input id="idEventCWD" type="checkbox" value="1" name="event_cwd" <?=(@$obj->event_cwd ? 'checked' : '')?>>
		</td>
		<td>
			<label for="idEventCWD"><?=$AppUI->_( 'Show only on Working Days' )?></label>
		</td>
	</tr>
	<tr>
		<td align="right">
			<input id="idMailAttendees" type='checkbox' name='mail_invited' checked="true">
		</td>
		<td>
			<label for="idMailAttendees"><?=$AppUI->_('Mail Attendees?')?></label>
		</td>
	</tr>
	</table>
	</td>
</tr>
<tr>
	<td colspan="2" align="right">
	<?php
		// $m does not equal 'calendar' here???
		require_once $AppUI->getSystemClass("CustomFields");
		$custom_fields = new CustomFields( 'calendar', 'addedit', $obj->event_id, "edit" );
		$custom_fields->printHTML();
	?>
	</td>
</tr>
</table>
</td>
<td valign="top" width="60%">
	<table cellspacing="0" cellpadding="3" border="0" width="95%">
	<tr>
		<td><?=$AppUI->_( 'Description' )?>:<br/>
			<textarea class="textarea" name="event_description" style="width: 100%" rows="5"><?=@$obj->event_description?></textarea>
		</td>
	</tr>
	<tr>
		<td>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td><?=$AppUI->_( 'Resources' )?>:</td>
				<td>&nbsp;</td>
				<td><?=$AppUI->_( 'Invited to Event' )?>:</td>
			</tr>
			<tr>
				<td width="45%"><?=arraySelect( $users, 'resources', 'id="idAvailables" style="width:100%" size="8" class="text" multiple="multiple" ', null)?></td>
				<td width="10%" align="center">
					<input type="image" src="images/next.gif" class="button" onClick="UserList('idMembers', 'idAvailables').join(); return false;" /><br/><br/>
					<input type="image" src="images/prev.gif" class="button" onClick="UserList('idMembers', 'idAvailables').part(); return false;" />
				</td>
				<td width="45%"><?=arraySelect( $assigned, 'assigned', 'id="idMembers" style="width:100%" size="8" class="text" multiple="multiple" ', null)?></td>
			</tr>
			</table>
		</td>
	</tr>
	</table>
</td>
</tr>
</form>
</table>
